﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class ProductCategoryController : Controller
    {
        //
        // GET: /ProductCategory/
        private readonly IProductCategoryRepository _productCategoryRepository;
        private readonly IUnitOfWork _unitOfWork;
        public ProductCategoryController(IProductCategoryRepository productCategoryRepository, IUnitOfWork unitOfWork)
        {
            _productCategoryRepository = productCategoryRepository;
            _unitOfWork = unitOfWork;
        }

        private IEnumerable<ProductCategoryModel> GetProductCategoryModels()
        {
            return from p in _productCategoryRepository.GetAll().AsQueryable()
                   select new ProductCategoryModel
                              {
                                  Name = p.Name,
                                  ID = p.ID,
                                  Remark = p.Remark
                              };
        }

        public ActionResult Index()
        {
            return View(GetProductCategoryModels());
        }
        [GridAction]
        public ActionResult TelerikAjax()
        {
            return View(new GridModel(GetProductCategoryModels()));
        }
        [HttpPost]
        [GridAction]
        public ActionResult Insert()
        {
            var productCategroy =new  ProductCategory();
            if(TryUpdateModel(productCategroy))
            {
                _productCategoryRepository.Add(productCategroy);
                _unitOfWork.Commit();
            }
            return View(new GridModel(GetProductCategoryModels()));
        }
        [HttpPost]
        [GridAction]
        public ActionResult Update(int id)
        {
            var productCategroy = _productCategoryRepository.GetById(id);
            if(TryUpdateModel(productCategroy))
            {
                _productCategoryRepository.Update(productCategroy);
                _unitOfWork.Commit();
            }
            return View(new GridModel(GetProductCategoryModels()));
        }
         [HttpPost]
        [GridAction]
        public ActionResult Delete(int id)
         {
             var productcategory = _productCategoryRepository.GetById(id);
             if(TryUpdateModel(productcategory))
             {
                 _productCategoryRepository.Delete(productcategory);
                 _unitOfWork.Commit();
             }
             return View(new GridModel(GetProductCategoryModels()));
         }

    }
}
